package com.lovenote.common.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.base.common.dao.IBaseDao;
import com.base.common.service.impl.BaseServiceImpl;
import com.base.common.util.Utils;
import com.lovenote.common.dao.IUserDao;
import com.lovenote.common.pojo.User;
import com.lovenote.common.service.IUserService;

@Service("userService")
public class UserServiceImpl extends BaseServiceImpl<User, Long> implements IUserService {

	private IUserDao userDao;
	
	@Autowired
	@Qualifier("userDao")
	@Override
	public void setBaseDao(IBaseDao<User, Long> baseDao) {
		this.baseDao = baseDao;
		this.userDao = (IUserDao) baseDao;
	}

	@Override
	public User authIdentity(String username, String password) {
		List<User> userList = this.userDao.loadByProperty("username", username);
		if (userList.size() != 1) {
			return null;
		}
		/* Get password from db */
		String passwordDB = userList.get(0).getPassword();
		
		if (passwordDB.equals(Utils.MD5(username + password).toLowerCase())) {
			return userList.get(0);
		}
		return null;
	}

}
